﻿using System;
using System.Web;
namespace Common
{
    public class StringHelper
    {
        /// <summary>
        /// SQL特殊字符替换
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string SpecialChar(string str)
        {
            if (!string.IsNullOrWhiteSpace(str))
            {
                return str.Replace("'", "''").Replace("\\", "\\\\");
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlDecode(string str)
        {
            if (!string.IsNullOrWhiteSpace(str))
            {
                return System.Web.HttpUtility.UrlDecode(str);
            }
            else
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// 编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlEncode(string str)
        {
            if (!string.IsNullOrWhiteSpace(str))
            {
                return System.Web.HttpUtility.UrlEncode(str);
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 按双字节截取字段
        /// </summary>
        /// <param name="s"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string bSubstring(string s, int length)
        {
            byte[] bytes = System.Text.Encoding.Unicode.GetBytes(s);
            int n = 0;  //  表示当前的字节数
            int i = 0;  //  要截取的字节数
            for (; i < bytes.GetLength(0) && n < length; i++)
            {

                //  偶数位置，如0、2、4等，为UCS2编码中两个字节的第一个字节
                if (i % 2 == 0)
                {
                    n++;      //  在UCS2第一个字节时n加1
                }
                else
                {

                    //  当UCS2编码的第二个字节大于0时，该UCS2字符为汉字，一个汉字算两个字节
                    if (bytes[i] > 0)
                    {
                        n++;
                    }
                }
            }

            //  如果i为奇数时，处理成偶数

            if (i % 2 == 1)
            {
                //  该UCS2字符是汉字时，去掉这个截一半的汉字

                if (bytes[i] > 0)
                    i = i - 1;

                 //  该UCS2字符是字母或数字，则保留该字符
                else
                    i = i + 1;
            }
            return System.Text.Encoding.Unicode.GetString(bytes, 0, i);
        }

        /// <summary>
        /// 生成随机数字组成的字符串
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string CreateRandomNumString(int length)
        {
            /*char[] chars = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
           'O','P','Q','R','S','T','U','V','W','X','Y','Z'
            ,'1','2','3','4','5','6','7','8','9','0'
            ,'a','b','c','d','e','f','g','h','i','j','k','l','m','n'
            ,'o','p','q','r','s','t','u','v','w','x','y','z'};*/
            char[] chars = new char[]{'1','2','3','4','5','6','7','8','9','0'};
            Random random = new Random(Guid.NewGuid().GetHashCode());
            string fileName = String.Empty;
            for (int i = 0; i < length; i++)
            {
                int index = random.Next(0, 9);
                fileName += chars[index];
            }
            return fileName;
        }
        /// <summary>
        /// 生成随机字母数字字符串
        /// </summary>
        public static string CreateRandomString(int length)
        {
            char[] chars = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
           'O','P','Q','R','S','T','U','V','W','X','Y','Z'
            ,'1','2','3','4','5','6','7','8','9','0'
            ,'a','b','c','d','e','f','g','h','i','j','k','l','m','n'
            ,'o','p','q','r','s','t','u','v','w','x','y','z'};
            Random random = new Random(Guid.NewGuid().GetHashCode());
            string fileName = String.Empty;
            for (int i = 0; i < length; i++)
            {
                int index = random.Next(0, 61);
                fileName += chars[index];
            }
            return fileName;
        }
    }
}
